package d.n.d.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import d.n.d.constant.Constant;
import d.n.d.domain.Account;
import d.n.d.domain.RespJSON;
import d.n.d.domain.User;
import d.n.d.service.AccountService;
import d.n.d.service.CustomerService;
import d.n.d.service.TransactionService;

/**
 * 
 * @author D.N.D
 * 
 */
@Controller
public class SupportController {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private AccountService accountService;

	@Autowired
	private TransactionService transactionService;

	private String error = null;
	private String message = null;
	private String validation = null;

	/**
	 * @author DucLV
	 * Get request transaction from account. <br/>
	 * 
	 * @param request
	 * @param accountNumber
	 * @return Account requesting.
	 */
	@RequestMapping(value = "/requestTransaction", method = RequestMethod.GET)
	public @ResponseBody Account requestTransaction(HttpServletRequest request,
			@RequestParam("accountNumber") String accountNumber) {

		Account account = accountService
				.findAccountInfoByAccountInfoNumber(accountNumber);
		account.getIdCardNumber().setAccounts(null);
		return account;
	}

	/**
	 * @author DucLV
	 * Confirm information after select add funds function. <br/>
	 * 
	 * @param request
	 * @param accountNumber
	 * @param moneyTransaction
	 * @return JSON Object.
	 */
	@RequestMapping(value = "/addFundConfirm", method = RequestMethod.POST)
	public @ResponseBody RespJSON addFundConfirm(HttpServletRequest request,
			@RequestParam("accountNumber") String accountNumber,
			@RequestParam("moneyAdd") String moneyTransaction) {

		return transactionService.confirmInfoMoneyAdd(accountNumber,
				moneyTransaction);
	}

	/**
	 * @author DucLV
	 * Execute ADD FUNDS into Database.<br/>
	 * 
	 * @param request
	 * @param accountNumber
	 * @param moneyTransaction
	 * @return JSON Object.
	 */
	@RequestMapping(value = "/addFundComplete", method = RequestMethod.POST)
	public @ResponseBody RespJSON addFundComplete(HttpServletRequest request,
			@RequestParam("moneyAdd") double moneyTransaction,
			@RequestParam("accountNumber") String accountNumber) {

		User userSupport = (User) request.getSession().getAttribute("user");

		return transactionService.executeMoneyAdd(accountNumber,
				moneyTransaction, userSupport);
	}

	/**
	 * <br>
	 * Confirm information after select withdraw function. <br/>
	 * 
	 * @param request
	 * @param moneyTransaction
	 * @return JSON Object.
	 */
	@RequestMapping(value = "/withdrawConfirm", method = RequestMethod.POST)
	public @ResponseBody RespJSON withdrawConfirm(HttpServletRequest request,
			@RequestParam("accountNumber") String accountNumber,
			@RequestParam("moneyWithdraw") String moneyTransaction1) {
		Account account = accountService
				.findAccountInfoByAccountInfoNumber(accountNumber);
		account.getIdCardNumber().setAccounts(null);
		double moneyRemainWithraw = 0.0;
		// /double Balance = account.getBalance();
		String strCheck = "";
		double balance = account.getBalance();
		boolean verifyWithdraw = transactionService.verifyWithdraw(
				moneyTransaction1, balance);
		if (verifyWithdraw == false) {
			strCheck = "validWithdraw";
		} else {
			moneyRemainWithraw = account.getBalance()
					- Double.parseDouble((String) moneyTransaction1);
		}
		RespJSON respJSON = new RespJSON(moneyTransaction1, account,
				moneyRemainWithraw, message, strCheck);
		return respJSON;
	}

	/**
	 * <br>
	 * Handle withdraw function.<br/>
	 * 
	 * @param model
	 * @param moneyTransaction
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/withdrawComplete", method = RequestMethod.POST)
	public @ResponseBody RespJSON withdrawComplete(Model model,
			@RequestParam("moneyWithdraw") double moneyTransaction1,
			@RequestParam("accountNumber") String accountNumber,
			HttpServletRequest request) {
		User userSupport = (User) request.getSession().getAttribute("user");
		Account account = accountService
				.findAccountInfoByAccountInfoNumber(accountNumber);
		account.getIdCardNumber().setAccounts(null);

		double balance = account.getBalance();
		String moneyTransactionWithdraw = Double.toString(moneyTransaction1);

		boolean verifyWithdraw = transactionService.verifyWithdraw(
				moneyTransactionWithdraw, balance);
		if (verifyWithdraw == false) {
			message = null;
			error = Constant.ERROR_TRANSACTION_WITHDRAW_FUND;
		} else {
			boolean doTransaction = transactionService
					.createWithdrawTransaction(moneyTransaction1, account,
							userSupport);
			if (doTransaction == false) {
				error = Constant.ERROR_TRANSACTION_WITHDRAW_FUND;
				message = null;
			} else {
				error = null;
				message = Constant.TRANSACTION_SUCCESS;
			}
		}
		RespJSON respJSON = new RespJSON(message, error, validation);
		return respJSON;
	}

	/**
	 * <br>
	 * Confirm information after select transfer function. <br/>
	 * 
	 * @param request
	 * @param moneyTransaction
	 * @param strReceiverAccountNumber
	 * @return
	 */
	@RequestMapping(value = "/transferConfirm", method = RequestMethod.GET)
	public @ResponseBody RespJSON transferConfirm(HttpServletRequest request,
			@RequestParam("moneyTransfer") String strMoneyTransfer,
			@RequestParam("accountNumber") String strAccountNumber,
			@RequestParam("receiverNumber") String strReceiverNumber) {

		return transactionService.confirmInfoMoneyTransfer(strAccountNumber,
				strMoneyTransfer, strReceiverNumber);
	}

	/**
	 * <br>
	 * Author: DucDS </br> <br>
	 * Handle transfer function.</br>
	 * 
	 * @param model
	 * @param moneyTransaction
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/transferComplete", method = RequestMethod.POST)
	public @ResponseBody RespJSON transferComplete(Model model,
			@RequestParam("moneyTransfer") String strMoneyTransfer,
			@RequestParam("accountNumber") String strAccountNumber,
			@RequestParam("receiverNumber") String strReceiverNumber,
			HttpServletRequest request) {

		HttpSession session = request.getSession();
		return transactionService.executeMoneyTransfer(strAccountNumber,
				strMoneyTransfer, strReceiverNumber,
				(User) session.getAttribute("user"));
	}
}
